package com.winchain.chainbill.acct.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.constant.rel.AccountConstants;
import com.shell.constant.rel.AssetCode;
import com.winchain.chainbill.acct.dao.CoopBankDao;
import com.winchain.chainbill.acct.po.CoopBank;
import com.winchain.chainbill.acct.service.CoopBankService;
import com.winchain.chainbill.acct.vo.CoopBankVo;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("coopBankService")
public abstract class CoopBankServiceImpl extends PersistentServiceImpl<CoopBank> implements CoopBankService {
	@Autowired
	private CoopBankDao coopBankDao;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private CusRecService cusRecService;

	@Pagination
	public List<CoopBankVo> selectCoopBankList(PageLimit limit, CoopBankVo coopBankVo) {
		String bankName = coopBankVo.getBankName();
		if (!StringUtil.emptyAndNull(bankName)) {
			coopBankVo.setBankName(StringUtil.getParameterLike(bankName));
		}
		coopBankVo.setCusId("platform");
		return this.coopBankDao.selectCoopBankList(coopBankVo);
	}

	public void saveCoopBank(CoopBankVo coopBankVo) {
		CoopBank coopBank = genCoopBank(coopBankVo);
		saveOrUpdate(coopBank);
		if (coopBankVo.isAdd()) {
			addSpclAcct(coopBankVo);
		}
	}

	public void changeState(String bankCode, String state) {
		CoopBank bank = (CoopBank) selectByKey(bankCode);
		bank.setState(state);
		updateNotNull(bank);

		boolean normal = "1".equals(state);
		List<AcctCard> cardList = getBankVirtualAccts(bankCode);
		for (AcctCard card : cardList) {
			if (normal) {
				card.setAcctSts("N");
			} else {
				card.setAcctSts("I");
			}
			this.billAcctService.updateCard(card);
		}
	}

	private List<AcctCard> getBankVirtualAccts(String bankCode) {
		AcctCard card = new AcctCard();
		card.setFiNo(bankCode);
		card.setAcctType("1");
		card.setOaaChnCode("B");
		return this.billAcctService.getCards(card);
	}

	public boolean checkAcctNo(String bankCode, String acctNo) {
		AcctBas bas = this.billAcctService.getAcctBas(acctNo);
		return (bas != null) && (!bas.getFiNo().equals(bankCode));
	}

	public boolean checkDepPri(String bankCode, int depPri) {
		CoopBank bank = new CoopBank();
		bank.setDepPri(Integer.valueOf(depPri));
		bank = (CoopBank) selectOne(bank);
		return (bank != null) && (!bank.getBankCode().equals(bankCode));
	}

	public boolean checkWdrlPri(String bankCode, int wdrlPri) {
		CoopBank bank = new CoopBank();
		bank.setWdrlPri(Integer.valueOf(wdrlPri));
		bank = (CoopBank) selectOne(bank);
		return (bank != null) && (!bank.getBankCode().equals(bankCode));
	}

	private void addSpclAcct(CoopBankVo coopBankVo) {
		AcctCard bas = new AcctCard();
		bas.setAcctNo(coopBankVo.getAcctNo());
		bas.setAcctNm(coopBankVo.getAcctName());
		bas.setHqFiNo(coopBankVo.getBankCode());
		bas.setHqFiNm(coopBankVo.getBankName());
		bas.setFiNo(coopBankVo.getBankCode());
		bas.setFiNm(coopBankVo.getBankDot());
		bas.setFiPrvnNm(coopBankVo.getBankProvince());
		bas.setFiCityNm(coopBankVo.getBankCity());

		CusRec cusRec = this.cusRecService.getPlatform(null);
		if (cusRec != null) {
			bas.setCustNm(cusRec.getCusName());
		} else {
			bas.setCustNm(AccountConstants.PLATFORM_NAME);
		}
		bas.setCustId("platform");

		bas.setAcctType("1");
		bas.setOaaChnCode("B");
		bas.setCustAcctType("C");
		bas.setCurrCode(AssetCode.CNY.name());
		this.billAcctService.addCard(bas);
	}

	private CoopBank genCoopBank(CoopBankVo coopBankVo) {
		String bankCode = coopBankVo.getBankCode();
		CoopBank bank = (CoopBank) selectByKey(bankCode);
		if (bank == null) {
			bank = new CoopBank();
			bank.setBankCode(bankCode);
			bank.setBankName(coopBankVo.getBankName());
			bank.setState("1");
			coopBankVo.setAdd(true);
		} else if ((!bank.getBankProvince().equals(coopBankVo.getBankProvince()))
				|| (bank.getBankCity() != coopBankVo.getBankCity())
				|| (!bank.getBankCity().equals(coopBankVo.getBankCity()))
				|| (!bank.getBankDot().equals(coopBankVo.getBankDot()))) {
			List<AcctCard> accts = getBankVirtualAccts(bankCode);
			for (AcctCard acct : accts) {
				acct.setHqFiNo(coopBankVo.getBankCode());
				acct.setHqFiNm(coopBankVo.getBankName());
				acct.setFiNo(coopBankVo.getBankCode());
				acct.setFiNm(coopBankVo.getBankDot());
				acct.setFiPrvnNm(coopBankVo.getBankProvince());
				acct.setFiCityNm(coopBankVo.getBankCity());
				this.billAcctService.update(acct);
			}
		}
		bank.setDepPri(coopBankVo.getDepPri());
		bank.setWdrlPri(coopBankVo.getWdrlPri());
		bank.setBankProvince(coopBankVo.getBankProvince());
		bank.setBankCity(coopBankVo.getBankCity());
		bank.setBankDot(coopBankVo.getBankDot());
		return bank;
	}
}
